<?php

/**
 * 模块下layout布局插件
 * 
 * @package Rt/Controller/Plugin
 * @uses Zend_Controller_Plugin_Abstract
 * @author Administrator
 * @version $Id: ModuleLayout.php 1945 2011-05-17 08:15:38Z wei.deng $
 */
class OP_Controller_Plugin_ModuleLayout extends Zend_Controller_Plugin_Abstract
{

    /**
     * Called before an action is dispatched by Zend_Controller_Dispatcher.
     *
     * This callback allows for proxy or filter behavior.  By altering the
     * request and resetting its dispatched flag (via
     * {@link Zend_Controller_Request_Abstract::setDispatched() setDispatched(false)}),
     * the current action may be skipped.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $_request)
    {
        $config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
        $moduleName = $_request->getModuleName();
        $layout = Zend_Layout::getMvcInstance();
        $filename = "";
        $moduleDir = Zend_Controller_Front::getInstance()->getModuleDirectory();

        if(isset($config[$moduleName]['resources']['layout']['layout'])){
            if($config[$moduleName]['resources']['layout']['layout']){
                $layout->setLayout(
                $config[$moduleName]['resources']['layout']['layout']);
                $layout->setLayoutPath(
                $moduleDir . DIRECTORY_SEPARATOR .
                $config[$moduleName]['resources']['layout']['layoutPath']);
            } else {
                $layout->disableLayout();
            }
        }elseif(isset($config['resources']['layout']['layout'])){
            $layout->setLayout($config['resources']['layout']['layout']);
            $filename = $config['resources']['layout']['layout'] . "." .
            $layout->getViewSuffix();
            if(file_exists(
            $moduleDir . DIRECTORY_SEPARATOR .
            $config['resources']['layout']['layoutPath'] .
            DIRECTORY_SEPARATOR . $filename)){
                $layout->setLayoutPath(
                $moduleDir . DIRECTORY_SEPARATOR .
                $config['resources']['layout']['layoutPath']);
            }else{
                $layout->setLayoutPath(
                APPLICATION_PATH .
                $config['resources']['layout']['layoutPath']);
            }
        }
    }
}
?>